package com.bjqg.bjqg01.bjqg.activity.longrange.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.activity.longrange.utils.OnSelectItemClickListener;

import java.util.ArrayList;

/**
 * Created by bjqg01 on 2018/9/14.
 */

public class SelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<ImageItem> mList;//当前选中的图片的绝对路径数组
    private OnSelectItemClickListener mListener;

    public onDeleteItemClickListener dListener;

    public interface onDeleteItemClickListener {
        void DeleteItemClickListener(View v, int i);
    }

    public void setOnDeleteItemClickListener(onDeleteItemClickListener dListener) {
        this.dListener = dListener;
    }

    public SelectAdapter(Context context, ArrayList<ImageItem> list) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mList = list;
    }

    public void setDataSource(ArrayList<ImageItem> list) {
        this.mList = list;
    }

    public void setOnItemClickListener(OnSelectItemClickListener onItemClickListener) {
        this.mListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectHolder(mInflater.inflate(R.layout.select_img_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        SelectHolder select = (SelectHolder) holder;
        final boolean isAddBtn;
        if (position == mList.size() - 1 && "".equals(mList.get(position).getImagePath())) {
            select.Delete_List_Item_Img.setVisibility(View.GONE);
            isAddBtn = true;
        } else {
            select.Delete_List_Item_Img.setVisibility(View.VISIBLE);
            isAddBtn = false;
        }
        select.img.setImageBitmap(mList.get(position).getBitmap());
        if (mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClickListener(holder.itemView, position, isAddBtn);
                }
            });
        }
        if (dListener != null) {
            ((SelectHolder) holder).Delete_List_Item_Img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dListener.DeleteItemClickListener(view, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private static class SelectHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView Delete_List_Item_Img;

        private SelectHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.Select_List_Item_Img);
            Delete_List_Item_Img = (ImageView) itemView.findViewById(R.id.Delete_List_Item_Img);
        }
    }


}
